package edu.gatech.TNE.wonderLab7000.person;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import edu.gatech.TNE.wonderLab7000.util.WonderLogger;
import oracle.jdbc.pool.OracleDataSource;

/**
 * Person class
 * 
 * @author Team NamingException
 *
 */
public class person {
	
	private int accessLevel;
	private String query, userName;
	private PreparedStatement statement;
    private Connection conn;
    private ResultSet result;
    public WonderLogger logger = new WonderLogger("mine");
	
	public person(String userName, int accessLevel)
	{
		this.userName = userName;
		this.accessLevel = accessLevel;
	}
	
	 /**
	 * Connects to the acme.gatech.edu database
	 * @throws SQLException
	 */
	public Connection connect() throws SQLException {
        OracleDataSource ods = new OracleDataSource();
        ods.setURL("jdbc:oracle:thin:ops$mdye3/wonderLab@acme.gatech.edu:1525:public");
        conn = ods.getConnection();
        return conn;
    }
	
	/**
	 * Username Getter
	 * 
	 * @return Person's username
	 */
	public String getUserName()
	{
		return userName;
	}
	
	/**
	 * Accesslevel Getter
	 * 
	 * @return Person's access level
	 */
	public int getAccessLevel()
	{
		return accessLevel;
	}
	
	/**
	 * Name Getter
	 * 
	 * @return Person's name
	 */
	public String getName() throws SQLException {
		
		query = "SELECT * FROM USERS WHERE USERID ='" + userName + "'";
		statement = conn.prepareStatement(query);
        try {
			result = statement.executeQuery();
		} catch (SQLException e) {
            conn.close();
            statement.close();
            result.close();
			System.out.println(e.toString());
			logger.error(e.toString());
		} 
		result.next();
		return result.getString("NAME");
		
	}
	
	public String[] getCourses() throws SQLException {
		String[] resultStr = null;
		conn = connect();
		query = "SELECT * FROM USERS WHERE USERID ='" + userName + "'";
		statement = conn.prepareStatement(query);
        try {
			result = statement.executeQuery();
		} catch (SQLException e) {
            conn.close();
            statement.close();
            result.close();
			System.out.println(e.toString());
			logger.error(e.toString());
		} 
		result.next();
		String courseString = result.getString("COURSES");
		if(courseString != null)
			resultStr = courseString.split(",");		
        conn.close();
        statement.close();
        result.close();
		return resultStr;
		
	}

}
